import { connect } from 'react-redux';
import UI from './UI';
import { Toast } from 'antd-mobile';
const mapStateToProps = (state) => {//给UI组件提供数据
  return {
    list: state.register.list,
    hasError: state.register.hasError,
    value: state.register.value,
    password: state.register.password,
  }
}
const mapDispatchToProps = (dispatch) => {//处理业务逻辑
  return {
    onErrorClick: () => {
      Toast.info('请输入有效的11位手机号');
    },
    RegisterFn (value, password, istrue) {
      // console.log(this)
      if(value === '' && password === ''){
        Toast.info('请输入用户名或者密码')
      } else if(istrue===true) {
        Toast.info('请输入正确的手机号')
      }else{
        // console.log('点击登录',value, password, istrue)
        fetch(`http://106.13.34.4:8080/new/login?username=${value}&password=${password}`)
          .then(res => res.json())
          .then(data => {
            // console.log(data)
            localStorage.setItem('uid',data.uid)
            if(data.code ===1){
              // console.log('跳到首页',this)
              this.history.goBack()
            } else if (data.code === 0 ) {
              Toast.info(data.msg)
            }
          })
      }
    },
    onChange: (value) => {
      if (value.replace(/\s/g, '').length < 11) {
        dispatch({
          type: 'CHANGE_HASERROR',
          data: true
        })
      } else {
        dispatch({
          type: 'CHANGE_HASERROR',
          data: false
        })
      }
      
      dispatch({
        type:'CHANGE_VALUE',
        data: value
      })
    },
    onChangePass (password) {
      // console.log(password)
      dispatch({
        type:'CHANGE_PASSWORD',
        data: password
      })
    }
  }
}
const Home =connect (
  mapStateToProps,
  mapDispatchToProps
)(UI)
export default Home